<template>
  <div>
    <div class="wid" ref="container">
      <div class="img-body">
        <el-tabs v-model="keyIndex" tab-position="right">
          <el-tab-pane v-for="(item,index) in tabList" :key="index" :label="item" :name="index.toString()">
          </el-tab-pane>
        </el-tabs>
        <div class="heeader flex-alc flex-btn">
          <h1>{{ listData.name }}</h1>
          <div class="btn-box">
            <el-button type="primary" icon="el-icon-document-copy" @click="copyAll(listData.data)">复制全部</el-button>
            <el-button type="success" icon="el-icon-paperclip" @click="optBtn(1,'https://gitee.com/webwlx/img-test')">源代码</el-button>
            <el-button type="info" icon="el-icon-edit" @click="codeFlag = true">生成图片</el-button>
          </div>
        </div>
        <div class="img-main images" v-viewer>
          <template v-for="(ele,key) in listData.data">
            <div class="img-box" :key="key">
              <div class="icon-box">
                <el-tooltip class="item" effect="dark" content="下载" placement="top-end">
                  <i class="el-icon-download" @click="optBtn(1,ele)"></i>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="查看" placement="top-end">
                  <i class="el-icon-zoom-in" @click="showImg(ele)" :preview="key"></i>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="复制" placement="top-end">
                  <i class="el-icon-paperclip" @click="copyText(ele,$event)"></i>
                </el-tooltip>
              </div>
              <img v-lazy="ele" :preview="key" :preview-text="ele+key" :alt="ele+key" />
            </div>
          </template>
        </div>
      </div>
    </div>

    <el-dialog title="生成占位图" :visible.sync="codeFlag" width="25%">
      <div class="from-box">
        <el-form ref="form" :model="codeImgFrom" label-width="80px">
          <el-form-item label="图片大小">
            <el-row :gutter="10">
              <el-col :span="10">
                <el-input v-model="codeImgFrom.width" placeholder="宽度" type="number"></el-input>
              </el-col>
              <el-col :span="2" style="text-align: center"><span>X</span></el-col>
              <el-col :span="10">
                <el-input v-model="codeImgFrom.height" placeholder="高度" type="number"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="显示文字">
            <el-input v-model="codeImgFrom.text" placeholder="显示文字 ( 默认为 宽x高 )"></el-input>
          </el-form-item>
          <el-form-item label="背景颜色">
            <el-color-picker v-model="codeImgFrom.bg"></el-color-picker>
          </el-form-item>
          <el-form-item label="文字颜色">
            <el-color-picker v-model="codeImgFrom.fg"></el-color-picker>
          </el-form-item>

        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="codeFlag = false">取 消</el-button>
        <el-button type="primary" @click="subBtn">生成</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { avaterData, petData, shopData, bigData, articleData, bannerData } from './data.js';

export default {
  data() {
    return {
      list: [
        { name: '头像', data: avaterData },
        { name: '宠物', data: petData },
        { name: '商品', data: shopData },
        { name: 'banner', data: bannerData },
        { name: '高清大图', data: bigData },
        { name: '详情页', data: articleData },
      ],
      codeImgFrom: {
        width:500,
        height:500,
        fg: '#666666',
        bg: '#cccccc'
      },
      codeFlag: false,
      keyIndex: 0,
    }
  },
  computed: {
    listData() {
      return this.list[this.keyIndex]
    },
    tabList() {
      return this.list.map(item => item.name)
    },
  },
  methods: {
    subBtn() {
      let { width, height, text, fg, bg } = JSON.parse(JSON.stringify(this.codeImgFrom))
      !width ? width = 500 : ''
      !height ? height = 500 : ''
      !text ? text = width + 'x' + height : ''
      fg = fg.substr(1)
      bg = bg.substr(1)
      let str = `https://iph.href.lu/${width}x${height}?text=${text}&fg=${fg}&bg=${bg}`
      window.open(str)
    },
    copyAll(data) {
      /* 复制全部 */
      let str = data.join(',')
      this.copyText(str)
    },
    showImg() {
      const viewer = this.$el.querySelector('.images').$viewer
      viewer.show()
    },
    copyText(url) {
      this.$copyText(url).then(() => {
        this.$message.success('复制成功')
      }, () => {
        this.$message.error('复制失败')
      })
    },
    optBtn(type, url) {
      // type  1 下载 2 查看 3 复制
      if (type === 1) {
        window.open(url)
      } else {
        this.$message.success('查看')
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.img-body {
  position: relative;
  .el-tabs {
    position: absolute;
    right: -120px;
    top: 20%;
  }
}

.viewer-title {
  margin: 0 5% 20px !important;
  font-size: 16px !important;
  opacity: 1 !important;
}
h1 {
  color: #333;
  display: block;
  margin: 40px 0 40px 0;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
  &:hover {
    color: blueviolet;
  }
}
.img-main {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  .img-box {
    overflow: hidden;
    border-radius: 10px;
    cursor: pointer;
    position: relative;
    width: 230px;
    height: 230px;
    margin-right: 10px;
    margin-bottom: 10px;
    border: 4px solid #ebeef5;
    box-sizing: border-box;
    background-color: rgba($color: #000000, $alpha: 1);
    .icon-box {
      position: absolute;
      right: 20px;
      bottom: 20px;
      color: #fff;
      z-index: 2;
      opacity: 0.8;
      transition: 0.3s;
      i {
        margin-left: 14px;
        font-size: 25px;
      }
    }
    &:hover {
      .icon-box {
        opacity: 1;
      }
      img {
        transform: scale(1.2);
        opacity: 0.5;
      }
    }
    img {
      width: 100%;
      transition: 0.3s;
      height: 100%;
      position: absolute;
      z-index: 1;
      object-fit: cover;
    }
  }
}
</style>